package com.qingneng.web;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;
import org.mvel2.MVEL;

import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ModelDriven;
import com.qingneng.service.sdk.IWorkBudgetService;
import com.qingneng.vo.BudgetItem;
import com.tbfs.common.BaseAction;
import com.tbfs.util.UtilTool;

public class CalItemAction extends BaseAction implements ModelDriven<BudgetItem>{
	
	public static final Logger logger  = Logger.getLogger(CalItemAction.class);
	
	private BudgetItem budgetItem  =  new BudgetItem();
	
	private IWorkBudgetService workBudgetService;
	
	public CalItemAction(){};
	
	public CalItemAction(IWorkBudgetService workBudgetService){
		this.workBudgetService = workBudgetService;
	};
	
	public String arg;
	
	public String getArg() {
		return arg;
	}

	public void setArg(String arg) {
		this.arg = arg;
	}

	//施工人员消耗
	public String exe_sgry_xh(){
		if(logger.isDebugEnabled()){
			logger.debug("+++ begin execute 计算 施工人员消耗 ++++");
		}
		logger.info("item_name:"+budgetItem.getBudget_item_name());
		//from db table 's budget_item  formula 
		List list = workBudgetService.getBudgetItem(budgetItem);
		
		String formual = "2+$arg$/30";
		double unit_price = 100;
		if(list!=null &&list.size()>0){
			BudgetItem  record = (BudgetItem) list.get(0);
			formual = record.getItem_formula();
			unit_price = UtilTool.string2doubleFormat(record.getItem_unit_price());
		}	
		logger.info("item_arg:"+arg);
		
		Map<String, Object> vars = new HashMap<String, Object>();   
        vars.put("$arg$", new Double(arg));   
        Double budget_do_quantities = (Double )MVEL.eval(formual, vars); 
        Double totalAmount = budget_do_quantities*unit_price;
        logger.info("budget_do_quantities:"+ UtilTool.doubleFormat(budget_do_quantities));
        budgetItem.setBudget_do_quantities(String.valueOf(UtilTool.doubleFormat(budget_do_quantities)));
        budgetItem.setTotalAmount(UtilTool.doubleFormat(totalAmount));
        if(logger.isDebugEnabled()){
			logger.debug("+++ end execute 计算 施工人员消耗 ++++");
		}
		return Action.SUCCESS;
	}
	
	public String exe_ztys(){
		if(logger.isDebugEnabled()){
			logger.debug("+++ begin execute 计算 渣土运输 ++++");
		}
		logger.info("item_name:"+budgetItem.getBudget_item_name());
		//from db table 's budget_item  formula 
		List list = workBudgetService.getBudgetItem(budgetItem);
		
		String formual = "$arg$*0.75";
		double unit_price = 5;
		if(list!=null &&list.size()>0){
			BudgetItem  record = (BudgetItem) list.get(0);
			formual = record.getItem_formula();
			unit_price = UtilTool.string2doubleFormat(record.getItem_unit_price());
		}	
			
		logger.info("item_arg:"+arg);
		Map<String, Object> vars = new HashMap<String, Object>();   
        vars.put("$arg$", new Double(arg));   
        Double budget_do_quantities = (Double )MVEL.eval(formual, vars); 
        Double totalAmount = budget_do_quantities*unit_price;
        logger.info("result:"+budget_do_quantities);
        budgetItem.setBudget_do_quantities(String.valueOf(UtilTool.doubleFormat(budget_do_quantities)));
        budgetItem.setTotalAmount(UtilTool.doubleFormat(totalAmount));
        if(logger.isDebugEnabled()){
			logger.debug("+++ end execute 计算 渣土运输  ++++");
		}
		return Action.SUCCESS;
	}
	
	
	
	
	public static void main(String args[]){
		String formual = "$arg$*0.75";	
		Map<String, Object> vars = new HashMap<String, Object>();   
        vars.put("$arg$", new Integer(100));   
        Double result = (Double )MVEL.eval(formual, vars);  		
		//String replacement = "111";
		//String exe =  formual.replaceAll("\\$arg\\$",replacement);
		System.out.print(result);
	}

	public BudgetItem getModel() {
		// TODO Auto-generated method stub
		return budgetItem;
	}

}
